using UnityEngine;
using Noise;
using Render;

public class PerlinShader : GUITextureNode
{
    public Material Material;

    public string PermTextureSizeProperty = "_PermTexSize";
    public int PermTextureSize = 256;

    public string PermTextureProperty = "_PermTex2D";
    public Texture2D PermTexture;

    public string GradTextureProperty = "_GradTex2D";
    public Texture2D GradTexture;

    public string PermGradTextureProperty = "_PermGradTex2D";
    public Texture2D PermGradTexture;

    public override void Awake()
    {
        base.Awake();

        Materials.SetFloat(PermTextureSizeProperty, PermTextureSize, Material);

        uint[] perm_array = PermutationArray.Perm2(PermTextureSize, PermTextureSize);
        PermTexture = Textures.Create2D(perm_array, PermTextureSize, false, true, 0, FilterMode.Point, TextureWrapMode.Repeat);
        Materials.SetTexture(PermTextureProperty, PermTexture, Material);

        uint[] grad_array = GradientArray.Grad2(PermTextureSize, PermTextureSize);
        GradTexture = Textures.Create2D(grad_array, PermTextureSize, false, true, 0, FilterMode.Point, TextureWrapMode.Repeat);
        Materials.SetTexture(GradTextureProperty, GradTexture, Material);

        uint[] perm_grad_array = GradientArray.PermGrad3(PermTextureSize);
        PermGradTexture = Textures.Create1D(perm_grad_array, PermTextureSize, false, true, 0, FilterMode.Point, TextureWrapMode.Repeat);
        Materials.SetTexture(PermGradTextureProperty, PermGradTexture, Material);        
    }

    public override void DrawGUI()
    {
        if (Event.current.type.Equals(EventType.Repaint))
            Graphics.DrawTexture(new Rect(PositionX, PositionY, Width, Height), Texture, Material);
        GUI.Label(new Rect(PositionX, PositionY+Height, Width, 50.0f), name);
    }

    protected override Texture2D CreateTexture()
    {
        return new Texture2D(TextureSize, TextureSize, TextureFormat.RGBA32, false);
    }
}